package org.lovelandhs.robotics.y2010.input;

import java.util.Vector;

/**
 *
 * @author tim
 */
public class Button {

	private int number;
	private String name;
	private boolean pressed;

	private Vector listeners;

	public Button(int number, String name, boolean pressed) {
		this.number = number;
		this.name = name;
		this.pressed = pressed;

		listeners = new Vector();
	}

	public Button(int number, String name) {
		this(number, name, false);
	}

	public Button(int number) {
		this(number, String.valueOf(number));
	}

	public int getNumber() {
		return number;
	}

	public String getName() {
		return name;
	}

	public boolean isPressed() {
		return pressed;
	}

	public void setPressed(boolean pressed) {
		boolean wasPressed = this.pressed;

		this.pressed = pressed;

		if (!wasPressed && pressed) {
			notifyPressed();
		} else if (wasPressed && !pressed) {
			notifyReleased();
		}
	}

	protected void notifyPressed() {
		for (int i = 0; i < listeners.size(); i++) {
			ButtonListener l = (ButtonListener) listeners.elementAt(i);

			l.onButtonPressed(this);
		}
	}

	protected void notifyReleased() {
		for (int i = 0; i < listeners.size(); i++) {
			ButtonListener l = (ButtonListener) listeners.elementAt(i);

			l.onButtonReleased(this);
		}
	}

	public void addListener(ButtonListener listener) {
		listeners.addElement(listener);
	}

	public void removeListener(ButtonListener listener) {
		listeners.removeElement(listener);
	}

}
